﻿using CPPEI.Coolzon.UI.XProperty;
using CPPEI.Coolzon.UI.XProperty.Converters;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CPPEI.Coolzon.UI.PetroleumDataPartLib.XProperty.Converters
{
    /// <summary>
    /// 瓦片地址转换
    /// </summary>
    public class XTileServiceConverter : IPropertyValueConverter
    {
        /// <summary>
        /// 转换
        /// </summary>
        /// <param name="context"></param>
        /// <param name="valueNode"></param>
        /// <returns></returns>
        public object Convert(ConvertContext context, Metadata.ValueNode valueNode)
        {
            List<TileService> services = new List<TileService>();
            foreach (Metadata.ValueNode vNode in valueNode.ValueNodes)
            {
                services.Add(new TileService()
                {
                    Url = vNode.Attributes["Url"],
                    Type = vNode.Attributes["Type"],
                });
            }
            return services;
        }

        public Metadata.ValueNode ConvertBack(ConvertContext context, object value)
        {
            List<TileService> tileServiceList = value as List<TileService>;
            if (tileServiceList == null || tileServiceList.Count == 0)
            {
                return null;
            }

            Metadata.ValueNode rootNode = new Metadata.ValueNode("TileServiceList");
            rootNode.Attributes = new Dictionary<string, string>();
            rootNode.ValueNodes = new List<Metadata.ValueNode>();

            foreach (var item in tileServiceList)
            {
                Metadata.ValueNode valueNode = new Metadata.ValueNode("TileService");
                valueNode.Attributes = new Dictionary<string, string>();
                valueNode.Attributes.Add("Url", System.Convert.ToString(item.Url));
                valueNode.Attributes.Add("Type", System.Convert.ToString(item.Type));
                rootNode.ValueNodes.Add(valueNode);
            }

            return rootNode;
        }
    }
}
